<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :title="title"
    :visible="visible"
    :width="700"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form-model ref="ruleForm"
                    :label-col="labelCol"
                    :model="model"
                    :rules="rules" :wrapper-col="wrapperCol">
        <a-form-model-item label="员工名称" prop="name">
          <a-input v-model="model.name" placeholder="请输入员工名称"/>
        </a-form-model-item>
        <a-form-model-item label="员工电话号码" prop="phone">
          <a-input v-model="model.phone" placeholder="请输入员工电话号码"/>
        </a-form-model-item>
        <a-form-model-item label="证件类型" prop="certType">
          <a-select v-model="model.certType" placeholder="请选择证件类型">
            <a-select-option value="1">
              身份证
            </a-select-option>
            <a-select-option value="2">
              护照
            </a-select-option>
            <a-select-option value="3">
              士官证
            </a-select-option>
            <a-select-option value="4">
              居住证
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="证件号码" prop="certNo">
          <a-input v-model="model.certNo" placeholder="请输入员工证件号码"/>
        </a-form-model-item>
        <a-form-model-item label="绑定账号" prop="sysUserId">
          <a-select
            :default-active-first-option="false"
            :filter-option="false"
            :not-found-content="null"
            :show-arrow="false"
            v-model="model.sysUserName"
            placeholder="请选择或输入用户名"
            show-search
            style="width: 100%"
            @change="handleChange"
            @search="handleSearch"
          >
            <a-select-option v-for="(d, index) in searchResult" :key="index" :value="d.sysUserId">
              {{ d.account }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction, postAction } from '@/api/manage'

export default {
  name: 'AgentModel',
  data() {
    return {
      title: '员工新增',
      visible: false,
      model: {},
      searchResult: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: '/sale/cxmSaleStaffInfo/addStaffInfo',
        edit: '/sale/cxmSaleStaffInfo/editStaffInfo'
      },
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        }
      },
      rules:
        {
          name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
          phone: [{ required: true, message: '请选择员工手机号码', trigger: 'blur' }],
          certType: [{ required: false, message: '请选择证件类型', trigger: 'blur' }],
          certNo: [{ required: false, message: '请输入证件号码', trigger: 'blur' }],
          sysUserId: [{ required: false, message: '请绑定账号', trigger: 'blur' }]
        }
    }
  },
  mounted() {
  },
  methods: {
    handleChange(value) {
      console.log(value)
      this.model.sysUserId = value
      this.model.sysUserName = ''   //这句不能删，需要先初始化后，在modal上才能赋值
      for (let i in this.searchResult) {
        if (this.searchResult[i].sysUserId == value) {
          this.model.sysUserName = this.searchResult[i].account
          console.log(this.model.sysUserName)
          break
        }
      }
    },
    handleSearch(value) {
      if (value) {
        getAction('/sale/cxmSaleStaffInfo/getAccount?account=' + value).then((res) => {
          console.log(res)
          if (res.success) {
            this.searchResult = res.result
          }
        })
      } else if (!value) {
        this.searchResult = []
      }
    },
    add() {
      const that = this
      that.edit({})
    },
    edit(record) {
      getAction('/sale/cxmSaleStaffInfo/getAccount').then((res) => {
        console.log(res)
        if (res.success) {
          this.searchResult = res.result
        }
      })
      console.log(record)
      this.form.resetFields()
      this.model = Object.assign({}, record)
      console.log(this.model)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue({})
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.ruleForm.validate(valid => {

        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          if (!this.model.id) {
            httpurl += this.url.add
          } else {
            httpurl += this.url.edit
          }
          postAction(httpurl, this.model)
            .then((res) => {
              console.log(res)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.form.resetFields()
              this.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style lang="less" scoped>
</style>